package com.javasm.sys.tree;

import com.javasm.gg.GoodsType;
import com.javasm.sys.SysMenu;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class TreeUtil {

    public static List<SysMenu> buildMenuTree(Collection<SysMenu> list, Integer pid) {
        List<SysMenu> r = new ArrayList<>();
        for (SysMenu menu : list) {
            if(pid.equals(menu.getPid())){
                Integer id = menu.getId();
                List<SysMenu> children = buildMenuTree(list, id);
                if(children!=null && children.size()>0){
                    menu.setChildren(children);
                }
                r.add(menu);
            }
        }
        return r;
    }

    public static List<GoodsType> buildGoodsTypeTree(Collection<GoodsType> list, String pid) {
        List<GoodsType> r = new ArrayList<>();
        for (GoodsType gt : list) {
            if(pid.equals(gt.getPid())){
                String id = gt.getId();
                List<GoodsType> children = buildGoodsTypeTree(list, id);
                if(children!=null && children.size()>0){
                    gt.setChildren(children);
                }
                r.add(gt);
            }
        }
        return r;
    }
}
